/**
 * 
 */
package org.alipay4j.loader;

import java.util.HashMap;
import java.util.Map;

import org.alipay4j.model.base.Token;
import org.apache.commons.lang.StringUtils;

/**
 * 默认Token加载器
 * @author 贺亮
 *
 */
public class DefaultTokenLoader implements ITokenLoader {

	/**  Token对象  */
    private Map<String, Token> tokenMap = new HashMap<String, Token>();
    
	@Override
	public Token get(String appid) {
		return (tokenMap.get(appid) == null
                || StringUtils.isEmpty(tokenMap.get(appid).getAppAuthToken())) ? null : tokenMap.get(appid);
	}

	@Override
	public void refresh(String appid, Token token) {
		if (null == token || StringUtils.isEmpty(token.getAppAuthToken())) {
            throw new IllegalStateException("access_token is null or empty");
        }
        this.tokenMap.put(appid, token);
	}

}
